import copy
import P30030_用状态转换图识别标识符操作符和数字 as P30030

TOKEN_TYPE = ("TOKEN_TYPE_NUMBER", "TOKEN_TYPE_OPERATOR", "TOKEN_TYPE_KEYWORD", "TOKEN_TYPE_VARIABLE")
LIST_OPERATOR = ('+','-','/','*','>','<','=','{','}','(',')',';')
TOKEN_TYPE_KEYWORD = ("var","int", "for", "while", "break")

def Tokenize(expression):
    """
    将给定的数学表达式字符串进行分词处理。
    
    Args:
        expression (str): 需要分词的数学表达式字符串。
    
    Returns:
        list: 分词后的结果列表，每个元素是一个字典，包含单词（"word"）和类型（"type"）两个键。
            单词类型为"TOKEN_TYPE_NUMBER"表示数字，为"TOKEN_TYPE_OPERATOR"表示操作符。
    
    Raises:
        SystemExit: 如果遇到错误的数字格式，则打印错误信息并退出程序。
    
    """
    Tokens = []
    ids = P30030.recognizer(expression)
    for i in range(len(ids)):
        if ids[i].isdigit():
            id = {"word":ids[i],"type":"TOKEN_TYPE_NUMBER"}
            Tokens.append(id)
        elif ids[i] in LIST_OPERATOR:
            id = {"word":ids[i],"type":"TOKEN_TYPE_OPERATOR"}
            Tokens.append(id)
        elif ids[i] in TOKEN_TYPE_KEYWORD:
            id = {"word":ids[i],"type":"TOKEN_TYPE_KEYWORD"}
            Tokens.append(id)
        else:
            id = {"word":ids[i],"type":"TOKEN_TYPE_VARIABLE"}
            Tokens.append(id)

    return Tokens
if __name__ == '__main__':
    code = "int var1; float var2;var1=5;var2=7.8;float c=var1*var2;"
    Tokens = Tokenize(code)
    for token in Tokens:
        print(token)

